/*
 *  data_structure/array_generallist/saddle_point/main.c 
 *
 *  (C) 2011 Harry Wei
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define	R	2
#define	C	2

extern time_t time(time_t *);
extern void srand(unsigned);

int main(void)
{
	int i;
	int row;
	int col;
	int temp;
	int flag = 1;
	int **matrix = NULL;

//	srand(time(NULL));
	matrix = (int **)malloc(sizeof(int *) * R);
	if (!matrix) {
		printf("Allocating Error!\n");
		return 1;
	}
	for (row = 0; row < C; row++) {
		matrix[row] = (int *)malloc(sizeof(int) * C);
		if (!matrix[row]) {
			printf("Allocating Error!\n");
			return 1;
		}
	}
	for (row = 0; row < R; row++)
		for (col = 0; col < C; col++) {
//			matrix[row][col] = rand() % 11;
			printf("please enter row %d, col %d's num:",
						row, col);
			(void)scanf("%d", &matrix[row][col]);
		}
	for (row = 0; row < R; row++)
		for (col = 0; col < C; col++) {
			printf("row %d col %d is %d\n", 
					row + 1, col + 1, matrix[row][col]);
		}
	row = 0;
	col = 0;
	do {
		flag = 1;
		col = 0;
		temp = matrix[row][col];
		for (i = 0; i < C; i++) {
			if (temp > matrix[row][i]) {
				temp = matrix[row][i];
				col = i;
			}
		}
		for (i = 0; i < R; i++) {
			if (temp < matrix[i][col]) {
				flag = 0;
			}
		}
		if (1 == flag) {
			printf("Saddle point is row %d col %d: %d\n", 
					row + 1, col + 1, matrix[row][col]);
		} else {
			printf("No saddle points in %d row!\n", row + 1);
		}
		row = row + 1;
	} while (row < R);
	return 0;
}
